/*
 * @Author: Kang
 * @Date: 2022-02-12 18:10:37
 * @Last Modified by: Kang
 * @LastEditTime: 2022-06-30 23:04:15
 */
module.exports = app => {
    const Sequelize = require('sequelize');
    class ExampleService extends app.Service {
        * index(request, id) {
            const Op = Sequelize.Op
            try {
                let current = parseInt(request.current) || 1;
                let pageSize = parseInt(request.pageSize) || 10;
                let where = {};
                let titles = request.title;
                if (titles) {
                    where.title = {
                        [Op.like]: '%' + titles + '%'
                    }
                }
                where.user_id = id
                console.log('where', where)
                const result = yield this.ctx.model.Example.findAndCountAll({
                    order: [['update_time', 'DESC']],
                    where: where,
                    offset: (current - 1) * pageSize,
                    limit: pageSize
                })
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * getListAll(request) {
            try {
                let current = parseInt(request.current) || 1;
                let pageSize = parseInt(request.pageSize) || 6;
                let where = {};
                where.user_id = request.user_id
                console.log('where', where)
                const result = yield this.ctx.model.Example.findAndCountAll({
                    order: [['update_time', 'DESC']],
                    where: where,
                    offset: (current - 1) * pageSize,
                    limit: pageSize
                })
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * create(request) {
            try {
                // console.log('request',request)
                request.text = request.text.replace(/http:\/\/49.233.199.152:7001/g, 'https://www.anhezd.com');
                const example = yield this.ctx.model.Example.create(request);
                let result = {};
                result.data = example;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * update(request) {
            try {
                console.log(request)
                const example = yield this.ctx.model.Example.findByPk(request.id);
                const { id, title, sorts, top_url, img_url, type, push_time, desc, cloum1, cloum2, cloum3, cloum4 } = request;
                let text = request.text.replace(/http:\/\/49.233.199.152:7001/g, 'https://www.anhezd.com');
                let res = yield example.update({
                    id,
                    title,
                    sorts,
                    top_url,
                    text,
                    img_url,
                    type,
                    push_time, desc, cloum1, cloum2, cloum3, cloum4
                });
                let result = {};
                result.data = res;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * destroy(id) {
            try {
                const example = yield this.ctx.model.Example.findByPk(id);
                const res = yield example.destroy();
                const result = {};
                result.data = res;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * top(id) {
            try {
                const example = yield this.ctx.model.Example.findByPk(id);
                let res = yield example.update({
                    id,
                    update_time: new Date().getTime()
                });
                let result = {};
                result.data = res;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }


        * getExampleInfo(request) {
            console.log(request.id)
            const res = yield this.ctx.model.Example.findOne({ 'where': { id: request.id } })
            return res;
        }


    }
    return ExampleService;
} 